/*
	Copyright 2008 Marco Mustapic
	
    This file is part of Agilar GWT Widgets.

    Agilar GWT Widgets is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Agilar GWT Widgets is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Agilar GWT Widgets.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.agilar.gwt.colorpicker;


import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ColorPicker extends Composite implements FocusListener, PopupListener, ClickListener
{
	private TextBox textBox = new TextBox();
	private ColorChooserPanel colorChooserPanel = new ColorChooserPanel();
	private FocusPanel colorPanel = new FocusPanel();

	private static class ColorPickerPopup extends PopupPanel
	{
		public ColorPickerPopup(ColorChooserPanel colorPicker)
		{
			super(true);
			setWidget(colorPicker);
		}
	}
	
	private ColorPickerPopup popup = new ColorPickerPopup(colorChooserPanel);
	
	public ColorPicker()
	{
		textBox.addStyleName("agilar-colorpicker-text");
		textBox.addFocusListener(this);
		textBox.setMaxLength(6);
		
		popup.addStyleName("agilar-colorpicker-popup");		
		popup.addPopupListener(this);
		
		colorPanel.addStyleName("agilar-colorpicker-colorPreview");
		colorPanel.addClickListener(this);
		colorPanel.addFocusListener(this);
		
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(textBox);
		hpanel.add(colorPanel);
		hpanel.addStyleName("agilar-colorpicker-widget");

		initWidget(hpanel);
	}
	
	public ColorPicker(String color)
	{
		this();
		setColor(color);
	}

	public void onFocus(Widget w)
	{
		showPopup();
	}

	public void onLostFocus(Widget w)
	{
	}
	
	public void setColor(String color)
	{
		textBox.setText(color);
		colorChooserPanel.setColor(color);
		colorPanel.getElement().setAttribute("style", "background-color: #"+colorChooserPanel.getColor());
	}
	
	public String getColor()
	{
		return colorChooserPanel.getColor();
	}

	public void onPopupClosed(PopupPanel popup, boolean autoClose)
	{
		textBox.setText(colorChooserPanel.getColor());
		colorPanel.getElement().setAttribute("style", "background-color: #"+colorChooserPanel.getColor());
	}

	public void onClick(Widget arg0)
	{
		showPopup();
	}

	private void showPopup()
	{
		colorChooserPanel.setColor(textBox.getText());
		popup.show();
		popup.setPopupPosition(textBox.getAbsoluteLeft(), textBox.getAbsoluteTop());
	}
}
